/**
 * Created by kinghackers on 2016/11/29.
 */

var express = require('express');
var path = require('path');
var ueditor = require('../my_modules/m_ueditor');
var router = express.Router();

router.get('/', function(req, res, next) {
    res.render('ueditor', { title: 'ueditor示例' });
});

router.use("/ue", ueditor(path.join(__dirname, '../public'), function (req, res, next) {
    console.info("我的path："+path.join(__dirname, 'public'));
    //客户端上传文件设置
    var ActionType = req.query.action;
    if (ActionType === 'uploadimage' || ActionType === 'uploadfile' || ActionType === 'uploadvideo') {
        var file_url = '/upload/ueditor/image/';//默认图片上传地址
        /*其他上传格式的地址*/
        if (ActionType === 'uploadfile') {
            file_url = '/upload/ueditor/file/'; //附件
        }
        if (ActionType === 'uploadvideo') {
            file_url = '/upload/ueditor/video/'; //视频
        }
        console.info("我的file_url："+file_url);
        res.ue_up(file_url); //你只要输入要保存的地址 。保存操作交给ueditor来做
        res.setHeader('Content-Type', 'text/html');
    }
    //  客户端发起图片列表请求
    else if (req.query.action === 'listimage') {
        var dir_url = '/upload/ueditor/image/';
        res.ue_list(dir_url); // 客户端会列出 dir_url 目录下的所有图片
    }
    // 客户端发起其它请求
    else {
        // console.log('config.json')
        res.setHeader('Content-Type', 'application/json');
        res.redirect('/libs/ueditor/nodejs/config.json');
    }
}));

module.exports = router;